﻿using System;
using EnvDTE;
using EnvDTE80;
using VSLangProj;
using System.Runtime.Serialization;
using Microsoft.Practices.RecipeFramework;
using ProjectBase.Guidance.Helpers;
using System.IO;

namespace ProjectBase.Guidance.References
{
    /// <summary>
    /// UnBoundRecipe that allows to be executed only on Solution Folders
    /// </summary>
    [Serializable]
    public class CreateDataReference: UnboundRecipeReference
    {

        /// <summary>
        /// Constructor of the SolutionFolderRecipeReference that must specify the 
        /// recipe name that will be used by the reference
        /// </summary>
        /// <param name="recipe"></param>
        public CreateDataReference(string recipe)
            : base(recipe)
        {
        }

        /// <summary>
        /// Returns a friendly name as Any Solution folder
        /// </summary>
        public override string AppliesTo
        {
            get { return " .Data project, or any folder in .Data project"; }
        }

        /// <summary>
        /// Performs the validation of the item passed as target
        /// Returns true if the reference is allowed to be executed in the target
        /// that is if the target is a solution folder
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public override bool IsEnabledFor(object target)
        {
            Project project = target as Project;
            if(project == null)
            {
                ProjectItem projectItem = target as ProjectItem;                
                if(projectItem != null)
                {
                    project = projectItem.ContainingProject;
                    string path = VisualStudioHelper.CurrentTargetPath(project.DTE);
                    if (Path.HasExtension(path))
                    {
                        return false;
                    }                    
                }
            }
            if (project == null)
            {
                return false;
            }            
            return SolutionHelper.IsDataProject(project)
                && project.Kind == PrjKind.prjKindCSharpProject 
                && (int)project.Properties.Item("OutputType").Value == (int)prjOutputType.prjOutputTypeLibrary;
        }

        #region ISerializable Members

        /// <summary>
        /// Required constructor for deserialization.
        /// </summary>
        protected CreateDataReference(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        #endregion ISerializable Members
    }
}
